package com.android.example.myapplication;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity8 extends AppCompatActivity implements View.OnClickListener {
    private Button btn_icon;//声明一个按钮对象
    private Drawable drawable;//声明一个图形对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main8);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //从布局文件中获取按钮控件
        btn_icon = (Button) findViewById(R.id.btn_icon);
        //从资源文件中获取图形对象
        drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        // 必须设置图片大小，否则不显示图片
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        //通过按钮分别演示上下左右4个方向
        Button btn_left = (Button) findViewById(R.id.btn_left);
        Button btn_top = (Button) findViewById(R.id.btn_top);
        Button btn_right = (Button) findViewById(R.id.btn_right);
        Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
        btn_left.setOnClickListener(this);
        btn_top.setOnClickListener(this);
        btn_right.setOnClickListener(this);
        btn_bottom.setOnClickListener(this);

    }
    //触发监听事件，启动onClick方法
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_left) {
            //设置按钮控件内部文字左边的图标
            btn_icon.setCompoundDrawables(drawable, null, null, null);
        } else if (v.getId() == R.id.btn_top) {
            //设置按钮控件内部文字上边的图标
            btn_icon.setCompoundDrawables(null, drawable, null, null);
        } else if (v.getId() == R.id.btn_right) {
            //设置按钮控件内部文字右边的图标
            btn_icon.setCompoundDrawables(null, null, drawable, null);
        } else if (v.getId() == R.id.btn_bottom) {
            //设置按钮控件内部文字下边的图标
            btn_icon.setCompoundDrawables(null, null, null, drawable);
        }
    }


}